package cn.bluemobi.dylan.step.ui.login;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpActivity;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.login.LoginContract;
import cn.bluemobi.dylan.step.presenter.login.LoginPresenter;
import cn.bluemobi.dylan.step.ui.TabMainActivity;

public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.btn_login)
    Button btn_login;

    @Override
    protected int getLayout() {
        return R.layout.login;
    }

    @Override
    protected void initView() {
        super.initView();
        initToolbar();
    }

    private void initToolbar() {
        //设置toolbartoolbar_back
        setSupportActionBar(mToolbar);
        toolbar_back.setVisibility(View.GONE);
        tv_title.setText("登录");
    }

    @OnClick({R.id.btn_login,R.id.tv_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if(logingValidate()){
                    mPresenter.login(et_phone.getText().toString().trim(),et_code.getText().toString().trim());
                }
                break;
            case R.id.tv_code:
                if(phoneValidate()){
                    mPresenter.httpGetCode(et_phone.getText().toString().trim());
                }
                break;
        }
    }

    /**
     * 手机号验证
     * @return
     */
    private boolean phoneValidate(){
        if(TextUtils.isEmpty(et_phone.getText().toString().trim())){
            Toast.makeText(this,"请输入手机号",Toast.LENGTH_LONG).show();
            return false;
        }else if(et_phone.getText().toString().trim().length()!=11){
            Toast.makeText(this,"手机号格式不正确",Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    /**
     * 登录验证
     * @return
     */
    private boolean logingValidate(){
        if(phoneValidate()){
            if(!TextUtils.isEmpty(tv_code.getText().toString().trim())){
                return true;
            }
        }
        return false;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {

    }

    @Override
    public void httpLoginSuccess(User user) {
        startActivity(new Intent().setClass(this, TabMainActivity.class));
        finish();
    }

    @Override
    public void httpRequestCodeSuccess(User user) {
        et_code.setText(user.phoneCode);
    }
}
